<template>
	<view class="home-container">
		<view class="search-box">
			<my-search :radius="4" @click="gotoSearch"></my-search>
		</view>
		<swiper class="swiper-container" circular="true" :interval="3000" :duration="1000" indicator-dots="true" indicator-color="rgba(255, 255, 255, .8)" indicator-active-color="#00FFFF" autoplay="true">
		  <swiper-item v-for="(item, index) in swiperList" :key="index">
		    <navigator :url="item.bannerUrl">
				<view class="swiper-item" >
					<image :src="'http://121.37.218.148:8080/images/' + item.bannerImg"></image>
				</view>
			</navigator>
		  </swiper-item>
		</swiper>
		<view class="seckill-container" v-if="seckillGoods">
			<view class="countdown-wrapper">
				<h2>秒杀商品</h2>
				<!-- <uni-countdown color="" background-color="" :day="5" :hour="24" :minute="0" :second="0"></uni-countdown> -->
			</view>
			<scroll-view scroll-x="true" class="seckill-goodslist">
				<view class="goods-item" v-for="item in seckillGoods" :key="item.goodsId">
					<navigator :url="`/subcontract/goods_detail/goods_detail?goodsId=${item.goodsId}`">
						<view class="img-box">
							<image :src="url + item.goodsImg"></image>
						</view>
						<view class="goods-info">
							<view class="description">
								{{ item.goodsName }}
							</view>
							<view class="goods-price">
								￥<span>{{ item.goodsPrice }}</span>
							</view>
						</view>
					</navigator>
				</view>
			</scroll-view>
		</view>
		<view class="production-recommend">
			<!-- <h2>推荐</h2> -->
			<view class="production-list">
				<view class="production-item" v-for="item in goodsList" :key="item.goodsId">
					<navigator :url="`/subcontract/goods_detail/goods_detail?goodsId=${item.goodsId}`">
						<view class="production-image">
							<image :src="url + item.goodsImg"></image>
						</view>
						<view class="production-desc">
							{{ item.goodsName }}
						</view>
						<view class="production-price">
							￥<span>{{ item.goodsPrice }}</span>
						</view>
					</navigator>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				goodsList: [],
				swiperList: [],
				url: '',
				queryParams: {
					page: 1,
					pageSize: 6,
				},
				totalCount: 0,
				loading: false,
				seckillGoods: [],
			};
		},
		onLoad() {
			this.getGoodsList()
			this.getSwiper()
			this.especialGoods()
			this.url = this.$imageUrl
		},
		onPullDownRefresh() {
			this.queryParams.page = 1
			this.goodsList = []
			this.loading = false
			this.getGoodsList(() => {
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if(this.queryParams.page * this.queryParams.pageSize >= this.totalCount) return uni.$showMsg('数据加载完毕 T_T')
			if(this.loading) return
			this.queryParams.page++
			this.getGoodsList()
		},
		methods: {
			gotoSearch() {
				uni.navigateTo({
					url: '/subcontract/search/search'
				})
			},
			gotoGoods() {
				uni.navigateTo({
					url: '/subcontract/goods_detail/goods_detail'
				})
			},
			async getGoodsList(callback) {
				 this.loading = true
				 const { data:res } = await uni.$http.get(`/goods/page`, this.queryParams)
				 this.loading = false
				 callback && callback()
				 console.log('res', res);
				 this.goodsList = [...this.goodsList, ...res.data.records]
				 this.totalCount = res.data.total
				 console.log('goodsList',this.goodsList);
			},
			async getSwiper() {
				const { data: res } = await uni.$http.get('/banners/swiper')
				console.log(res);
				this.swiperList = res.data
			},
			async especialGoods() {
				const { data: res } = await uni.$http.get('/goods?seckill=1')
				this.seckillGoods = res.data
			}
		}
	}
</script>

<style lang="scss">
	.home-container {
		width: 100%;
		// height: 100%;
		box-sizing: border-box;
		padding-bottom: 50px;
	}

	.search-box {
		// position: fixed;
		position: sticky;
		top: 44px;
		left: 0;
		width: 100%;
		z-index: 999;
	}

	.swiper-container {
		width: 100%;
		height: 300rpx;
		background-color: #eee;
		swiper {
			width: 100%;
			height: 100%;
		}
		navigator {
			width: 100%;
			height: 100%;
		}
		.swiper-item {
			background-color: aqua;
			width: 100%;
			height: 100%;
			image {
				width: 100%;
				height: 100%;
			}
		}
	}
	
	.countdown-wrapper {
		font-weight: bold;
	}

	.seckill-container {
		// width: 100%;
		margin: 20rpx;
		border-radius: 8rpx;
		padding: 20rpx;
		background-color: #fff;

		.countdown-wrapper {
			display: flex;
			margin-bottom: 20rpx;
			justify-content: center;
		}

		/deep/ .uni-countdown.data-v-6b44ea80 {
			display: flex;
			// justify-content: center;
		}

		/deep/ scroll-view ::-webkit-scrollbar {
			display: none;
		}
		.seckill-goodslist {
			display: flex;
			white-space: nowrap;
			justify-content: space-around;
			.goods-item {
				width: 200rpx;
				display: inline-block;
				padding: 8rpx;
				border: #f0f0f0 solid 1px;
				&:not(:last-child) {
					border-right: none;
				}
				.img-box {
					width: 200rpx;
					height: 200rpx;
					box-sizing: border-box;
					border: #f0f0f0 solid 1px;
					image {
						width: 100%;
						height: 100%;
						display: block;
					}
				}

				.goods-info {
					width: 100%;

					.description {
						font-size: 12px;
						white-space: pre-wrap;
						margin-top: 8rpx;
						margin-bottom: 10rpx;
						overflow: hidden; //多出的隐藏
						//text-overflow: ellipsis;//多出部分用...代替
						display: -webkit-box; //定义为盒子模型显示
						-webkit-line-clamp: 2; //用来限制在一个块元素显示的文本的行数
						-webkit-box-orient: vertical; //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
					}

					.goods-price {
						font-size: 9px;
						font-weight: bold;
						span {
							font-size: 13px;
						}
					}
				}
			}
		}
	}

	.production-recommend {
		// padding-bottom: 50px;
		.production-list {
			width: 100%;
			display: flex;
			flex-wrap: wrap;

			.production-item {
				width: 50%;
				padding: 20rpx;
				box-sizing: border-box;
				background-color: #fff;
				border: 1rpx #eee solid;
				border-bottom: none;
				border-right: none;

				.production-image {
					width: 100%;
					height: 300rpx;

					image {
						width: 100%;
						height: 300rpx;
						display: block;
					}
				}

				.production-desc {
					font-size: 14px;
					margin-top: 20rpx;
					overflow: hidden; //多出的隐藏
					//text-overflow: ellipsis;//多出部分用...代替
					display: -webkit-box; //定义为盒子模型显示
					-webkit-line-clamp: 2; //用来限制在一个块元素显示的文本的行数
					-webkit-box-orient: vertical; //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
				}

				.production-price {
					margin-top: 16rpx;
					font-size: 14px;
					font-weight: bold;

					span {
						font-size: 18px;
					}
				}
			}
		}
	}
</style>
